package com.shenmazong.serveredustudent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenmazong.serveredustudent.mapper.*;
import com.shenmazong.serveredustudent.pojo.TbExamGrade;
import com.shenmazong.serveredustudent.pojo.TbStage;
import com.shenmazong.serveredustudent.service.TbExamGradeService;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.ListParamVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 成绩表实现类
 */
@Service
public class TbExamGradeServiceImpl extends ServiceImpl<TbExamGradeMapper, TbExamGrade>
    implements TbExamGradeService{

    @Autowired
    private TbExamGradeMapper examGradeMapper;

    @Autowired
    private TbStageMapper stageMapper;



    @Override
    public ResultResponse getStudentGradePage(ListParamVo listParamVo) {

        Page<TbExamGrade> tbExamGradePage = new Page<>(listParamVo.getCurrent(), listParamVo.getSize());

        Object collageId =  listParamVo.getParamMap().get("collageId");
        //根据学院Id
        if( collageId!=null ){

            LambdaQueryWrapper<TbExamGrade> gradeLambdaQueryWrapper = new QueryWrapper<TbExamGrade>().lambda().eq(TbExamGrade::getSchoolId, collageId);
            Page<TbExamGrade> tbExamGradePage1 = examGradeMapper.selectPage(tbExamGradePage, gradeLambdaQueryWrapper);
            return ResultResponse.SUCCESS(tbExamGradePage1);

        }

        Object majorId =  listParamVo.getParamMap().get("majorId");

        if( majorId!=null ){

            LambdaQueryWrapper<TbStage> eq = new QueryWrapper<TbStage>().lambda().eq(TbStage::getMajorId, majorId);

            List<TbStage> stages = stageMapper.selectList(eq);

            if(stages.size()==0){
                return ResultResponse.FAILED(-1,"参数异常");
            }

            String courseIds = "";

            for (TbStage stage : stages) {
                String s = stage.getStageId().toString();
                courseIds += ","+s;
            }
            courseIds = courseIds.substring(1);

            Page<TbExamGrade> tbExamGradePage1 = examGradeMapper.getStudentGradeByCourseIds(tbExamGradePage,courseIds);

            return ResultResponse.SUCCESS(tbExamGradePage1);


        }

        Object courseId =  listParamVo.getParamMap().get("courseId");

        if(courseId!=null){

            LambdaQueryWrapper<TbExamGrade> gradeLambdaQueryWrapper = new QueryWrapper<TbExamGrade>().lambda().eq(TbExamGrade::getCourseId, courseId);
            Page<TbExamGrade> tbExamGradePage1 = examGradeMapper.selectPage(tbExamGradePage, gradeLambdaQueryWrapper);
            return ResultResponse.SUCCESS(tbExamGradePage1);

        }



        Object clazzId = listParamVo.getParamMap().get("clazzId");

        if(clazzId!=null){

            LambdaQueryWrapper<TbExamGrade> gradeLambdaQueryWrapper = new QueryWrapper<TbExamGrade>().lambda().eq(TbExamGrade::getClazzId, clazzId);
            Page<TbExamGrade> tbExamGradePage1 = examGradeMapper.selectPage(tbExamGradePage, gradeLambdaQueryWrapper);
            return ResultResponse.SUCCESS(tbExamGradePage1);

        }

        return ResultResponse.FAILED(-1,"参数异常");

    }

    @Override
    public ResultResponse getStudentGrade(ListParamVo listParamVo) {

        Page<TbExamGrade> page = new Page<>(listParamVo.getCurrent(), listParamVo.getSize());
        Map<String, Object> paramMap = listParamVo.getParamMap();
        Page<TbExamGrade> examGradePage = examGradeMapper.getStudentGrade(page,paramMap);


        return null;
    }


}




